package com.xiaoxin.suma.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.xiaoxin.suma.R;
import com.xiaoxin.suma.utils.ActivityManager;
import com.xiaoxin.suma.utils.DataManger;
import com.xiaoxin.suma.utils.GsonUtil;

import java.lang.reflect.Type;

import butterknife.ButterKnife;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.Subscriptions;

/**
 * Created by ljm on 2017/5/23.
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected View root;
    protected Context mContext;
    protected Toolbar toolbar;
    protected TextView rightButton;
    protected TextView title;
    protected SPUtils spUtils;
    protected String Token;
    protected String userName;
    protected CompositeSubscription subscriptions = Subscriptions.from();
    public final String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        root = View.inflate(this, getLayoutId(), null);
        if (showActionBar()) {
            if (root instanceof LinearLayout) {
                toolbar = (Toolbar) View.inflate(this, R.layout.action_bar, null);
                title = (TextView) toolbar.findViewById(R.id.title);
                title.setText(getTitle());
                rightButton = (TextView) toolbar.findViewById(R.id.btn_right);
                ((LinearLayout) root).addView(toolbar, 0);
                toolbar.setNavigationOnClickListener(v -> onBackPressed());
            } else {
                throw new RuntimeException("跟布局必须是LineLayout");
            }
        }
        setContentView(root);
        ButterKnife.bind(this);
        mContext = this;
        spUtils = DataManger.spUtils;
        initData();
        initView();
        ActivityManager.getInstance().addActivity(this);
    }

    protected void initView() {
    }

    protected void initData() {
        Token = DataManger.getToken();
        userName = DataManger.getUserName();
    }

    protected abstract int getLayoutId();

    @Override
    public void setTitle(CharSequence titleS) {
        super.setTitle(titleS);
        if (title != null) {
            title.setText(titleS);
        }
    }

    public void startActivity(Class cls, Object... object) {
        Intent intent = new Intent(this, cls);
        for (int i = 0; i < object.length; i++) {
            intent.putExtra("data" + i, GsonUtil.defaultGson().toJson(object[i]));
        }
        startActivity(intent);
    }

    public void startActivity(int resultCode, Class cls, Object... object) {
        Intent intent = new Intent(this, cls);
        for (int i = 0; i < object.length; i++) {
            intent.putExtra("data" + i, GsonUtil.defaultGson().toJson(object[i]));
        }
        startActivityForResult(intent, resultCode);
    }


    public <T> T getData(Class<T> cls) {
        String str = getIntent().getStringExtra("data0");
        if (str == null) return null;
        return GsonUtil.defaultGson().fromJson(str, cls);
    }

    public <T> T getData(Class<T> cls, int pos) {
        String str = getIntent().getStringExtra("data" + pos);
        if (str == null) return null;
        return GsonUtil.defaultGson().fromJson(str, cls);
    }

    public <T> T getData(Type cls, int pos) {
        String str = getIntent().getStringExtra("data" + pos);
        if (str == null) return null;
        return GsonUtil.defaultGson().fromJson(str, cls);
    }

    public void setResultData(Class cls, Object o) {
        String json = GsonUtil.defaultGson().toJson(o, cls);
        setResultData(json);
    }

    public void setResultData(String data) {
        Intent intent = new Intent();
        intent.putExtra("RESULT", data);
        setResult(RESULT_OK, intent);
    }

    public <T> T getResultData(Intent intent, Class<T> cls) {
        String json = intent.getStringExtra("RESULT");
        if (json == null) return null;
        return GsonUtil.defaultGson().fromJson(json, cls);
    }

    public boolean showActionBar() {
        return true;
    }

    @Override
    protected void onDestroy() {
        if (subscriptions != null && !subscriptions.isUnsubscribed()) {
            subscriptions.unsubscribe();
        }
        ActivityManager.getInstance().removeActivity(this);
        super.onDestroy();
    }
}
